package org.csu.forum.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.forum.domain.PO.*;
import org.csu.forum.domain.VO.FollowPostVO;
import org.csu.forum.mapper.*;
import org.csu.forum.service.PostManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class PostManageServiceImpl implements PostManageService {
    @Autowired
    PostMapper postMapper;

    @Autowired
    FileMapper fileMapper;

    @Autowired
    TitlepostMapper titlepostMapper;

    @Autowired
    UserTitlePostMapper userTitlePostMapper;

    @Autowired
    UserSendPostMapper userSendPostMapper;

    @Autowired
    FollowpostMapper followpostMapper;

    @Autowired
    ReplyContentMapper replyContentMapper;


    @Override
    public int addTitlepost(Titlepost titlepost, int userid) {

        titlepostMapper.insert(titlepost);

        QueryWrapper<Titlepost>titlepostQueryWrapper = new QueryWrapper<>();
        titlepostQueryWrapper.eq("content",titlepost.getContent()).eq("zone_id",titlepost.getZoneid());
        titlepost = titlepostMapper.selectOne(titlepostQueryWrapper);


        UserTitlePost userTitlePost = new UserTitlePost();
        userTitlePost.setUserid(userid);
        userTitlePost.setPostid(titlepost.getPostid());
        userTitlePostMapper.insert(userTitlePost);

        return titlepost.getPostid();
    }

    @Override
    public int addPost(Post post, int userid , int followpost_floor , int titlepost_id) {
        postMapper.insert(post);
        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.eq("content",post.getContent()).eq("zoneid",post.getZoneid());
        post = postMapper.selectOne(postQueryWrapper);

        UserSendPost userSendPost = new UserSendPost();
        userSendPost.setUserid(userid);
        userSendPost.setPostid(post.getPostid());
        userSendPostMapper.insert(userSendPost);

        Followpost followpost = new Followpost();
        followpost.setTitlepostId(titlepost_id);
        followpost.setFollowpostId(post.getPostid());
        followpost.setFollowpostFloor(followpost_floor);
        followpostMapper.insert(followpost);

        return post.getPostid();
    }

    @Override
    public List<Titlepost> getTitlepostList(int zoneid) {
        QueryWrapper<Titlepost>titlepostQueryWrapper = new QueryWrapper<>();
        titlepostQueryWrapper.eq("zone_id",zoneid);
        return titlepostMapper.selectList(titlepostQueryWrapper);
    }

    @Override
    public List<FollowPostVO> getFollowPostVOList(int postid) {
        List<FollowPostVO>followPostVOList = new ArrayList<>();
        QueryWrapper<Followpost>followpostQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Files>fileQueryWrapper = new QueryWrapper<>();


        followpostQueryWrapper.eq("titlepost_id",postid);
        List<Followpost>followpostList = followpostMapper.selectList(followpostQueryWrapper);

        Iterator<Followpost>followpostIterator = followpostList.listIterator();

        while(followpostIterator.hasNext()){
            FollowPostVO followPostVO = new FollowPostVO();

            Followpost followpost = followpostIterator.next();
            followPostVO.setFollowpost(followpost);

            postQueryWrapper.clear();
            postQueryWrapper.eq("post_id",followpost.getFollowpostId());
            Post post = postMapper.selectOne(postQueryWrapper);
            followPostVO.setPost(post);

            fileQueryWrapper.clear();
            fileQueryWrapper.eq("file_id",post.getFileid());
            Files files = fileMapper.selectOne(fileQueryWrapper);
            followPostVO.setFiles(files);

            followPostVO.setPatameter();

            followPostVOList.add(followPostVO);
        }

        return followPostVOList;
    }

    @Override
    public int insertReplyContent(ReplyContent replyContent, int replyerid, Date time) {
        replyContent.setReplyerid(replyerid);
        replyContent.setTime(time);
        replyContentMapper.insert(replyContent);

        QueryWrapper<ReplyContent>replyContentQueryWrapper = new QueryWrapper<>();
        replyContentQueryWrapper.eq("post_id",replyContent.getPostid()).eq("content",replyContent.getContent());
        replyContent = replyContentMapper.selectOne(replyContentQueryWrapper);
        return replyContent.getReplyid();
    }

    @Override
    public List<ReplyContent> getReplyContentList(int postid) {
        QueryWrapper<ReplyContent>replyContentQueryWrapper =new QueryWrapper<>();
        replyContentQueryWrapper.eq("post_id",postid);
        List<ReplyContent>replyContents = replyContentMapper.selectList(replyContentQueryWrapper);
        Iterator<ReplyContent> iterator = replyContents.iterator();
        while(iterator.hasNext()){
            ReplyContent replyContent = iterator.next();
            replyContent.setPostContent(this.getPostByPostId(postid).getContent());
        }
        return replyContents;
    }

    @Override
    public Post getPostByPostId(int postid) {
        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.eq("post_id",postid);
        return postMapper.selectOne(postQueryWrapper);
    }

    @Override
    public void insertFile(Files files) {
        fileMapper.insert(files);
    }


}
